/**
 * 产品价格表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author duanzucai
 * @since 2019/11/26
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit'], function (exports) {
    var $ = layui.$;
    var table = layui.table;
    var laypage = layui.laypage;
    let layer = layui.layer;
    var form = layui.form;
    let index;
    let flag = "";

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-190',
        done: function (res) {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
            res.data.forEach(function (item,index) {
                if(item.status == 1){
                    layui.$('#zmn-table').next('div').find(".layui-table-body tbody tr[data-index='"+index+"'] td[data-field='status']").css({'color': '#DCDCDC' });
                }
            });
        }
    });

    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    // 导出excel
    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event == "CUSTOM_EXPORT") {
            console.info( $("#zmn-form-filter").serialize());
            // let name ='单券优惠.xlsx';
            // let arr=$("#zmn-form-filter").serializeArray();
            // let fields=new Object;
            // $.each(arr,function(k,v){
            //     fields[v.name]=v.value;
            // });
            zmnUtil.downloadLimitByConfig("/product/tariff/item/outputExcel?" + $("#zmn-form-filter").serialize(), "执行价格.xlsx", function () {
                layer.close();
            });
            return;
        }
    });
    //重置
    form.on('submit(zmn-form-reset)', function (obj) {
        form.val("zmn-form-filter", {
            "bizType": 1
            ,"tariffType": 1
            ,"showType": 1
        });
        //主动触发change
        $('#channelId').siblings("div.layui-form-select").find('dl dd:eq(0)').click();
        $('#brandId').siblings("div.layui-form-select").find('dl dd:eq(0)').click();

        return false;
    });

    /***********************以下为联动相关****************************************/

    function getRemnoteData(dataParam){
        return  $.ajax({
            async: false,
            type: "POST", url: "/product/tariff/item/loadChannelProducts.action",
            contentType: "application/json", dataType: "json",data:JSON.stringify(dataParam)
        });

    }

    //渠道联动
    form.on("select(channelId)", function (obj) {
        let channelId = obj.value;
        let bizType = $("#bizType").val();
        let tariffType = $("#tariffType").val();
        let showType = $("#showType").val();
        let dataParam = {channelId:channelId,flag:"1",bizType:bizType,tariffType:tariffType,showType:showType};
        getRemnoteData(dataParam).done(function (json) {
            if (json.status == 200) {
                let data = json.data;
                //城市
                let citys = data.baseAreas;
                //总的服务分类和产品一二级分类
                let categoryVOS = data.servCategories;
                let categOneList = data.categOneList;
                let categList = data.categList;
                // let brands = data.brands;
                let productList = data.productList;
                renderSelect(citys,"cityId","cityName","cityId",data.selectCity);
                renderSelect(categoryVOS,"servCategId","servCategName","servCategId");
                renderSelect(categOneList,"categOneId","categOneName","categOneId");
                renderSelect(categList,"categId","categName","categId");
                // renderSelect(brands,"brandId","brandName","brandId");
                renderSelect(productList,"productId","productName","productId",false,false);
            }
        });
    });
    //城市联动
    form.on("select(cityId)", function (obj) {
        let cityId = obj.value;
        let channelId = $("#channelId").val();
        let bizType = $("#bizType").val();
        let tariffType = $("#tariffType").val();
        let showType = $("#showType").val();
        let dataParam = {channelId:channelId,cityId:cityId,bizType:bizType,tariffType:tariffType,showType:showType};
        getRemnoteData(dataParam).done(function (json) {
            let data = json.data;
            //总的服务分类和产品一二级分类
            let categoryVOS = data.servCategories;
            let categOneList = data.categOneList;
            let categList = data.categList;
            let productList = data.productList;
            // let brands = data.brands;
            renderSelect(categoryVOS,"servCategId","servCategName","servCategId");
            renderSelect(categOneList,"categOneId","categOneName","categOneId");
            renderSelect(categList,"categId","categName","categId");
            // renderSelect(brands,"brandId","brandName","brandId");
            renderSelect(productList,"productId","productName","productId",false,false);
        })
    });
    //业务类型联动产品
    form.on("select(bizType)", function (obj) {
        let channelId = $("#channelId").val();
        let tariffType = $("#tariffType").val();
        let cityId = $("#cityId").val();
        let bizType = obj.value;
        let showType = $("#showType").val();
        let dataParam = {channelId:channelId,cityId:cityId,bizType:bizType,tariffType:tariffType,showType:showType};
        getRemnoteData(dataParam).done(function (json) {
            let data = json.data;
            //总的服务分类和产品一二级分类
            let categoryVOS = data.servCategories;
            let categOneList = data.categOneList;
            let categList = data.categList;
            let productList = data.productList;
            // let brands = data.brands;
            // renderSelect(brands,"brandId","brandName","brandId");
            renderSelect(categoryVOS,"servCategId","servCategName","servCategId");
            renderSelect(categOneList,"categOneId","categOneName","categOneId");
            renderSelect(categList,"categId","categName","categId");
            renderSelect(productList,"productId","productName","productId",false,false);
        })
    });
    //产品类型
    form.on("select(showType)", function (obj) {
        let channelId = $("#channelId").val();
        let bizType = $("#bizType").val();
        let tariffType = $("#tariffType").val();
        let cityId = $("#cityId").val();
        let showType = obj.value;
        let dataParam = {channelId:channelId,cityId:cityId,bizType:bizType,tariffType:tariffType,showType:showType};
        getRemnoteData(dataParam).done(function (json) {
            let data = json.data;
            //总的服务分类和产品一二级分类
            let categoryVOS = data.servCategories;
            let categOneList = data.categOneList;
            let categList = data.categList;
            let productList = data.productList;
            // let brands = data.brands;
            // renderSelect(brands,"brandId","brandName","brandId");
            renderSelect(categoryVOS,"servCategId","servCategName","servCategId");
            renderSelect(categOneList,"categOneId","categOneName","categOneId");
            renderSelect(categList,"categId","categName","categId");
            renderSelect(productList,"productId","productName","productId",false,false);
        })
    });
    //服务分类联动
    form.on("select(servCategId)", function (obj) {
        let servCategId = obj.value;
        let channelId = $("#channelId").val();
        let bizType = $("#bizType").val();
        let tariffType = $("#tariffType").val();
        let cityId = $("#cityId").val();
        let showType = $("#showType").val();

        let dataParam = {channelId:channelId,cityId:cityId,bizType:bizType,tariffType:tariffType,showType:showType,servCategId:servCategId};
        getRemnoteData(dataParam).done(function (json) {
            let data = json.data;
            let categOneList = data.categOneList;
            let categList = data.categList;
            let productList = data.productList;
            // let brands = data.brands;
            // renderSelect(brands,"brandId","brandName","brandId");
            renderSelect(categOneList,"categOneId","categOneName","categOneId");
            renderSelect(categList,"categId","categName","categId");
            renderSelect(productList,"productId","productName","productId",false,false);
        });
    });
    //产品一级分类联动
    form.on("select(categOneId)", function (obj) {
        let servCategId = $("#servCategId").val();
        let channelId = $("#channelId").val();
        let bizType = $("#bizType").val();
        let tariffType = $("#tariffType").val();
        let cityId = $("#cityId").val();
        let showType = $("#showType").val();
        let categOneId = obj.value;

        let dataParam = {channelId:channelId,cityId:cityId,bizType:bizType,tariffType:tariffType,showType:showType,servCategId:servCategId,categOneId:categOneId};
        getRemnoteData(dataParam).done(function (json) {
            let data = json.data;
            let productList = data.productList;
            let categList = data.categList;
            // let brands = data.brands;
            // renderSelect(brands,"brandId","brandName","brandId");
            renderSelect(categList,"categId","categName","categId");
            renderSelect(productList,"productId","productName","productId",false,false);
        });
    });
    //产品二级分类联动
    form.on("select(categId)", function (obj) {
        let servCategId = $("#servCategId").val();
        let channelId = $("#channelId").val();
        let bizType = $("#bizType").val();
        let tariffType = $("#tariffType").val();
        let cityId = $("#cityId").val();
        let showType = $("#showType").val();
        let categOneId = $("#categOneId").val();
        let categId = obj.value;
        let dataParam = {channelId:channelId,cityId:cityId,bizType:bizType,tariffType:tariffType,showType:showType,servCategId:servCategId,categOneId:categOneId,categId:categId};
        getRemnoteData(dataParam).done(function (json) {
            let data = json.data;
            let productList = data.productList;
            // let brands = data.brands;
            // renderSelect(brands,"brandId","brandName","brandId");
            renderSelect(productList,"productId","productName","productId",false,false);
        });
    });
    function renderSelect(items,id,attrName,attrValue,selectVal,needNullSelect){
        let optionHtml = "";
        if(needNullSelect){
            optionHtml += "<option value=''>请选择</option>"
        }
        for (let i = 0; i < items.length; i++) {
            optionHtml += '<option value="' + items[i][attrValue] + '">' + items[i][attrName] + '</option>';
        }
        $("#"+id).html(optionHtml);
        if(selectVal){
            $("#"+id).find("option[value='"+selectVal+"']").attr("selected","selected");
        }
        form.render("select");
    }

    //对外暴露的接口
    exports('serv_product_tariff_item_price_list', {});
});